#!/bin/sh
#
# Copyright (C) 2000-2023 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Shell script to update SQLite3 tables from Bacula
#
OLDVERSION=1022
NEWVERSION=1026

echo " "
echo "This script will update a Bacula SQLite3 database from version 12-14,$OLDVERSION to $NEWVERSION"
echo "	which is needed to convert from any Bacula Communty to version 15.0.x"
echo " "

bindir=@SQLITE_BINDIR@
PATH="$bindir:$PATH"
cd @working_dir@
db_name=@db_name@

DBVERSION=`sqlite3 ${db_name}.db <<END
SELECT VersionId FROM Version LIMIT 1;
END
`
if [ "$DBVERSION" -ne $OLDVERSION ] ; then
  if [ "$DBVERSION" -lt 12 -o "$DBVERSION" -gt $NEWVERSION ] ; then
    echo " "
    echo "The existing database is version $DBVERSION !!"
    echo "This script can only update an existing version 12, 13, 14 or 1014-$OLDVERSION database to version $NEWVERSION."
    echo "Error. Cannot upgrade this database."
    echo " "
    exit 1
  fi
fi

if [ "$DBVERSION" = 12 ] ; then
sqlite3 $* ${db_name}.db <<END-OF-DATA
BEGIN;

CREATE TABLE RestoreObject (
   RestoreObjectId INTEGER,
   ObjectName TEXT DEFAULT '',
   RestoreObject TEXT DEFAULT '',
   PluginName TEXT DEFAULT '',
   ObjectLength INTEGER DEFAULT 0,
   ObjectFullLength INTEGER DEFAULT 0,
   ObjectIndex INTEGER DEFAULT 0,
   ObjectType INTEGER DEFAULT 0,
   FileIndex INTEGER UNSIGNED DEFAULT 0,
   ObjectCompression INTEGER DEFAULT 0,
   JobId INTEGER UNSIGNED REFERENCES Job NOT NULL,
   PRIMARY KEY(RestoreObjectId)
   );
CREATE INDEX restore_jobid_idx ON RestoreObject (JobId);

UPDATE Version SET VersionId=13;
COMMIT;

END-OF-DATA
DBVERSION=13
fi

if [ "$DBVERSION" = 13 ] ; then

sqlite3 $* ${db_name}.db <<END-OF-DATA
BEGIN;

ALTER TABLE File ADD COLUMN DeltaSeq smallint default 0;
UPDATE Version SET VersionId=14;

COMMIT;

END-OF-DATA

DBVERSION=14

fi

if [ "$DBVERSION" = 14 ] ; then
    sqlite3 $* ${db_name}.db  <<END-OF-DATA
INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
   ('I', 'Incomplete Job',25);
UPDATE Version SET VersionId=1014;
END-OF-DATA
DBVERSION=1014
fi

if [ "$DBVERSION" = 1014 ] ; then
    sqlite3 $* ${db_name}.db  <<END-OF-DATA
.bail on
ALTER TABLE Media ADD COLUMN VolABytes BIGINT UNSIGNED DEFAULT 0;
ALTER TABLE Media ADD COLUMN VolAPadding BIGINT UNSIGNED DEFAULT 0;
ALTER TABLE Media ADD COLUMN VolHoleBytes BIGINT UNSIGNED DEFAULT 0;
ALTER TABLE Media ADD COLUMN VolHoles INTEGER UNSIGNED DEFAULT 0;
ALTER TABLE Pool ADD COLUMN CacheRetention BIGINT DEFAULT 0;
UPDATE Version SET VersionId=1015;
END-OF-DATA
DBVERSION=1015
fi

# Upgrade from the community edition
# 15 to 1017 migration
if [ "$DBVERSION" -eq 15 -o "$DBVERSION" -eq 16 ]; then
    # In version 16, the upgrade 1018-1019 is already done
    if [ "$DBVERSION" -eq 16 ]; then
        SKIP1018=1
    fi

    if ! sqlite3 $* ${db_name}.db  <<END-OF-DATA
.bail on
BEGIN;

DROP INDEX inx3;
DROP INDEX file_jpf_idx;

CREATE TABLE file_temp (
   FileId INTEGER,
   FileIndex INTEGER DEFAULT 0,
   JobId INTEGER UNSIGNED  REFERENCES Job NOT NULL,
   PathId INTEGER UNSIGNED REFERENCES Path NOT NULL,
   Filename TEXT NOT NULL DEFAULT '',
   DeltaSeq SMALLINT UNSIGNED DEFAULT 0,
   MarkId INTEGER UNSIGNED DEFAULT 0,
   LStat VARCHAR(255) NOT NULL,
   MD5  VARCHAR(255) NOT NULL,
   PRIMARY KEY (FileId)
   );

INSERT INTO file_temp (FileId, FileIndex, JobId, PathId, Filename, DeltaSeq,
		       MarkId, LStat, Md5)
   SELECT FileId, FileIndex, JobId, PathId, Filename.Name, DeltaSeq, 
	  MarkId, LStat, Md5
    FROM File JOIN Filename USING (FilenameId);

DROP TABLE Filename;
DROP TABLE File;

ALTER TABLE file_temp RENAME TO File;
CREATE INDEX inx3 ON File (JobId);
CREATE INDEX file_jpf_idx ON File (JobId, PathId, Filename);

ANALYZE File;

DROP TABLE UnsavedFiles;
CREATE TABLE UnsavedFiles (
   UnsavedId INTEGER,
   JobId INTEGER UNSIGNED REFERENCES Job NOT NULL,
   PathId INTEGER UNSIGNED REFERENCES Path NOT NULL,
   Filename TEXT NOT NULL,
   PRIMARY KEY (UnsavedId)
   );

UPDATE Version SET VersionId = 1017;

COMMIT;
END-OF-DATA
    then
	echo "File update for Bacula SQLite tables."
	exit 1
    fi
    echo "Upgrade of the File table succeeded. Version 1017"
    DBVERSION=1017
fi

if [ "$DBVERSION" = 1015 ] ; then
    sqlite3 $* ${db_name}.db  <<END-OF-DATA
.bail on
begin;
CREATE TABLE file_temp (
   FileId INTEGER,
   FileIndex INTEGER UNSIGNED NOT NULL,
   JobId INTEGER UNSIGNED REFERENCES Job NOT NULL,
   PathId INTEGER UNSIGNED REFERENCES Path NOT NULL,
   Filename TEXT NOT NULL DEFAULT '',
   DeltaSeq SMALLINT UNSIGNED DEFAULT 0,
   MarkId INTEGER UNSIGNED DEFAULT 0,
   LStat VARCHAR(255) NOT NULL,
   MD5 VARCHAR(255) NOT NULL,
   PRIMARY KEY(FileId) 
   );

INSERT INTO file_temp (FileId, FileIndex, JobId, PathId, Filename, DeltaSeq,
		       MarkId, LStat, Md5, llll)
   SELECT FileId, FileIndex, JobId, PathId, Filename.Name, DeltaSeq, 
	  MarkId, LStat, Md5
    FROM File JOIN Filename USING (FilenameId);

DROP TABLE File;
DROP TABLE Filename;

ALTER TABLE file_temp RENAME TO File;

CREATE INDEX inx3 ON File (JobId);
CREATE INDEX file_jpf_idx ON File (JobId, PathId, Filename);

ALTER TABLE Job ADD COLUMN FileTable text default 'File';
ALTER TABLE JobHisto ADD COLUMN FileTable text default 'File';
UPDATE Version SET VersionId = 1016;
commit;
END-OF-DATA
DBVERSION=1016
fi

if [ "$DBVERSION" = 1016 ] ; then
    sqlite3 $* ${db_name}.db  <<END-OF-DATA
.bail on
ALTER TABLE Snapshot ADD COLUMN JobId integer default 0;
ALTER TABLE Snapshot ADD COLUMN FileSetId integer default 0;
UPDATE Version SET VersionId=1017;
END-OF-DATA
DBVERSION=1017
fi

if [ "$DBVERSION" = 1017 ] ; then
    sqlite3 $* ${db_name}.db  <<END-OF-DATA
.bail on
CREATE TABLE FileMedia
(
    JobId	      integer	  not null,
    FileIndex	      integer	  not null,
    MediaId	      integer	  not null,
    BlockAddress      bigint	  default 0,
    RecordNo	      integer	  default 0,
    FileOffset	      bigint	  default 0
);
CREATE INDEX file_media_idx on FileMedia (JobId, FileIndex);
UPDATE Version SET VersionId=1018;
END-OF-DATA
DBVERSION=1018
fi

if [ "$DBVERSION" -eq 1018 -a "$SKIP1018" = 1 ]; then
    # From version 16, the upgrade 1018-1019 is already done
    if sqlite3 $* ${db_name}.db <<END-OF-DATA
UPDATE Version SET VersionId=1019;
END-OF-DATA
    then
	echo "Update of Bacula SQLITE tables 1018 to 1019 succeeded."
        DBVERSION=1019
    else
	echo "Update of Bacula PostgreSQL tables 1018 to 1019 failed."
	exit 1
    fi
fi

if [ "$DBVERSION" -eq 1018 ] ; then
    if sqlite3 $* ${db_name}.db  <<END-OF-DATA
.bail on
begin;
CREATE TABLE basefiles_temp (
   BaseId BIGINT,
   BaseJobId INTEGER UNSIGNED REFERENCES Job NOT NULL,
   JobId INTEGER UNSIGNED REFERENCES Job NOT NULL,
   FileId INTEGER UNSIGNED REFERENCES File NOT NULL,
   FileIndex INTEGER UNSIGNED,
   PRIMARY KEY(BaseId)
   );

INSERT INTO basefiles_temp (BaseId, BaseJobId, JobId,
	FileId, FileIndex) 
   SELECT BaseId, BaseJobId, JobId, FileId, FileIndex
    FROM BaseFiles;

DROP TABLE BaseFiles;
ALTER TABLE basefiles_temp RENAME TO BaseFiles;

ALTER TABLE Media RENAME TO Media_tmp;
DROP INDEX inx8;
DROP INDEX inx9;

CREATE TABLE Media (
   MediaId INTEGER,
   VolumeName VARCHAR(128) NOT NULL,
   Slot INTEGER DEFAULT 0,
   PoolId INTEGER UNSIGNED REFERENCES Pool DEFAULT 0,
   MediaType VARCHAR(128) NOT NULL,
   MediaTypeId INTEGER UNSIGNED REFERENCES MediaType DEFAULT 0,
   LabelType TINYINT DEFAULT 0,
   FirstWritten DATETIME DEFAULT 0,
   LastWritten DATETIME DEFAULT 0,
   LabelDate DATETIME DEFAULT 0,
   VolJobs INTEGER UNSIGNED DEFAULT 0,
   VolFiles INTEGER UNSIGNED DEFAULT 0,
   VolBlocks INTEGER UNSIGNED DEFAULT 0,
   LastPartBytes BIGINT UNSIGNED DEFAULT 0,
   VolMounts INTEGER UNSIGNED DEFAULT 0,
   VolBytes BIGINT UNSIGNED DEFAULT 0,
   VolABytes BIGINT UNSIGNED DEFAULT 0,
   VolAPadding BIGINT UNSIGNED DEFAULT 0,
   VolHoleBytes BIGINT UNSIGNED DEFAULT 0,
   VolHoles INTEGER UNSIGNED DEFAULT 0,
   VolType INTEGER UNSIGNED DEFAULT 0,
   VolParts INTEGER UNSIGNED DEFAULT 0,
   VolCloudParts INTEGER UNSIGNED DEFAULT 0,
   VolErrors INTEGER UNSIGNED DEFAULT 0,
   VolWrites BIGINT UNSIGNED DEFAULT 0,
   VolCapacityBytes BIGINT UNSIGNED DEFAULT 0,
   VolStatus VARCHAR(20) NOT NULL,
   Enabled TINYINT DEFAULT 1,
   Recycle TINYINT DEFAULT 0,
   ActionOnPurge     TINYINT	DEFAULT 0,
   CacheRetention BIGINT UNSIGNED DEFAULT 0,
   VolRetention BIGINT UNSIGNED DEFAULT 0,
   VolUseDuration BIGINT UNSIGNED DEFAULT 0,
   MaxVolJobs INTEGER UNSIGNED DEFAULT 0,
   MaxVolFiles INTEGER UNSIGNED DEFAULT 0,
   MaxVolBytes BIGINT UNSIGNED DEFAULT 0,
   InChanger TINYINT DEFAULT 0,
   StorageId INTEGER UNSIGNED REFERENCES Storage DEFAULT 0,
   DeviceId INTEGER UNSIGNED REFERENCES Device DEFAULT 0,
   MediaAddressing TINYINT DEFAULT 0,
   VolReadTime BIGINT UNSIGNED DEFAULT 0,
   VolWriteTime BIGINT UNSIGNED DEFAULT 0,
   EndFile INTEGER UNSIGNED DEFAULT 0,
   EndBlock INTEGER UNSIGNED DEFAULT 0,
   LocationId INTEGER UNSIGNED REFERENCES Location DEFAULT 0,
   RecycleCount INTEGER UNSIGNED DEFAULT 0,
   InitialWrite DATETIME DEFAULT 0,
   ScratchPoolId INTEGER UNSIGNED REFERENCES Pool DEFAULT 0,
   RecyclePoolId INTEGER UNSIGNED REFERENCES Pool DEFAULT 0,
   Comment TEXT,
   PRIMARY KEY(MediaId)
   );

CREATE INDEX inx8 ON Media (PoolId);
CREATE INDEX inx9 ON Media (StorageId);


INSERT INTO Media (MediaId, VolumeName, Slot, PoolId, MediaType, MediaTypeId,
   LabelType, FirstWritten, LastWritten, LabelDate, VolJobs, VolFiles, VolBlocks,
   LastPartBytes, VolMounts, VolBytes, VolABytes, VolAPadding, VolHoleBytes,
   VolHoles, VolType, VolParts, VolCloudParts, VolErrors, VolWrites,
   VolCapacityBytes, VolStatus, Enabled, Recycle, ActionOnPurge, CacheRetention,
   VolRetention, VolUseDuration, MaxVolJobs, MaxVolFiles, MaxVolBytes, InChanger,
   StorageId, DeviceId, MediaAddressing, VolReadTime, VolWriteTime, EndFile,
   EndBlock, LocationId, RecycleCount, InitialWrite, ScratchPoolId, RecyclePoolId,
   Comment)
   SELECT MediaId, VolumeName, Slot, PoolId, MediaType, MediaTypeId,
   LabelType, FirstWritten, LastWritten, LabelDate, VolJobs, VolFiles, VolBlocks,
   0, VolMounts, VolBytes, VolABytes, VolAPadding, VolHoleBytes,
   VolHoles, VolParts, 0, VolCloudParts, VolErrors, VolWrites,
   VolCapacityBytes, VolStatus, Enabled, Recycle, ActionOnPurge, 0,
   VolRetention, VolUseDuration, MaxVolJobs, MaxVolFiles, MaxVolBytes, InChanger,
   StorageId, DeviceId, MediaAddressing, VolReadTime, VolWriteTime, EndFile,
   EndBlock, LocationId, RecycleCount, InitialWrite, ScratchPoolId, RecyclePoolId,
   Comment from Media_tmp;

DROP TABLE Media_tmp;

UPDATE Version SET VersionId=1019;
commit;
END-OF-DATA
    then
	echo "Update of Bacula SQLite3 tables 1018 to 1019 succeeded."
	    DBVERSION=1019
    else
	echo "Update of Bacula SQLite3 tables 1018 to 1019 failed."
	exit 1
    fi
fi

if [ "$DBVERSION" -eq 1019 ] ; then
    if sqlite3 $* ${db_name}.db <<END-OF-DATA
begin;
ALTER TABLE Pool ADD COLUMN MaxPoolBytes BIGINT DEFAULT 0;
ALTER TABLE Job ADD COLUMN PriorJob VARCHAR(128) DEFAULT '';
ALTER TABLE JobHisto ADD COLUMN PriorJob VARCHAR(128) DEFAULT '';
UPDATE Version SET VersionId=1020;
commit;
END-OF-DATA
    then
	echo "Update of Bacula SQLite3 tables 1019 to 1020 succeeded."
	DBVERSION=1020
    else
	echo "Update of Bacula SQLite3 tables 1019 to 1020 failed."
	exit 1
    fi
fi

if [ "$DBVERSION" -eq 1020 ] ; then
    if sqlite3 $* ${db_name}.db <<END-OF-DATA
UPDATE Version SET VersionId=1021;
END-OF-DATA
    then
	echo "Update of Bacula SQLite3 tables 1020 to 1021 succeeded."
	DBVERSION=1021
    else
	echo "Update of Bacula SQLite3 tables 1020 to 1021 failed."
	exit 1
    fi
fi

if [ "$DBVERSION" -eq 1021 ] ; then
    if sqlite3 $* ${db_name}.db <<END-OF-DATA
.bail on
begin;
CREATE TABLE TagJob
(
   JobId integer not null,
   Tag   text    not null,
   primary key (JobId, Tag)
);

CREATE TABLE TagClient
(
   ClientId integer not null,
   Tag      text    not null,
   primary key (ClientId, Tag)
);

CREATE TABLE TagMedia
(
   MediaId integer not null,
   Tag      text    not null,
   primary key (MediaId, Tag)
);

CREATE TABLE TagObject
(
   ObjectId integer not null,
   Tag      text    not null,
   primary key (ObjectId, Tag)
);

CREATE TABLE Object
(
   ObjectId     integer  not null,

   JobId        integer  not null,
   Path         text     not null,
   Filename     text     not null,
   PluginName   text     not null,

   ObjectType   text     not null,
   ObjectName   text     not null,
   ObjectSource text     not null,
   ObjectUUID   text     not null,
   ObjectSize   integer  not null,
   primary key (ObjectId)
);

create index object_jobid_idx on Object (JobId);
create index object_type_idx on Object  (ObjectType);
create index object_name_idx on Object  (ObjectName);
create index object_source_idx on Object  (ObjectSource);

CREATE TABLE Events
(
    EventsId          INTEGER,
    EventsCode        text        not null,
    EventsType	      text	  not null,
    EventsTime	      DATETIME,
    EventsInsertTime  DATETIME DEFAULT current_timestamp,
    EventsDaemon        text        default '',
    EventsSource      text        default '',
    EventsRef         text        default '',
    EventsText	      text	  not null,
    primary key (EventsId)
);
create index events_time_idx on Events (EventsTime);

UPDATE Version SET VersionId=1022;
commit;
END-OF-DATA
    then
	echo "Update of Bacula SQLite3 tables 1021 to 1022 succeeded."
	DBVERSION=1022
    else
	echo "Update of Bacula SQLite3 tables 1021 to 1022 failed."
	exit 1
    fi
fi

if [ "$DBVERSION" -eq 1022 ] ; then
    if sqlite3 $* ${db_name}.db <<END-OF-DATA
begin;
ALTER TABLE Object ADD COLUMN ObjectCategory text not null;
create index object_category_idx on Object  (ObjectCategory);
INSERT INTO Events (EventsCode, EventsType, EventsTime, EventsDaemon, EventsSource, EventsRef, EventsText)
  VALUES ('DU0001', 'catalog_update', strftime('%Y-%m-%d %H-%M-%S','now'), '*SHELL*', 'update_bacula_tables', 'pid$$', 'Catalog schema was updated to 1023');
UPDATE Version SET VersionId=1023;
commit;
END-OF-DATA
    then
	echo "Update of Bacula SQLite3 tables 1022 to 1023 succeeded."
	DBVERSION=1023
    else
	echo "Update of Bacula SQLite3 tables 1022 to 1023 failed."
	exit 1
    fi
fi

if [ "$DBVERSION" -eq 1023 ] ; then
    if sqlite3 $* ${db_name}.db <<END-OF-DATA
begin;
ALTER TABLE Object ADD COLUMN ObjectStatus char(1) default 'U';
ALTER TABLE Object ADD COLUMN ObjectCount integer default 1;
create index object_status_idx on Object  (ObjectStatus);
INSERT INTO Events (EventsCode, EventsType, EventsTime, EventsDaemon, EventsSource, EventsRef, EventsText)
  VALUES ('DU0001', 'catalog_update', strftime('%Y-%m-%d %H-%M-%S','now'), '*SHELL*', 'update_bacula_tables', 'pid$$', 'Catalog schema was updated to 1024');
UPDATE Version SET VersionId=1024;
commit;
END-OF-DATA
    then
	echo "Update of Bacula SQLite3 tables 1023 to 1024 succeeded."
	DBVERSION=1024
    else
	echo "Update of Bacula SQLite3 tables 1023 to 1024 failed."
	exit 1
    fi
fi

if [ "$DBVERSION" -eq 1024 ] ; then
    if sqlite3 $* ${db_name}.db <<END-OF-DATA
begin;
ALTER TABLE Client ADD COLUMN Plugins VARCHAR(1024) DEFAULT '';

CREATE TABLE MetaEmail
(
    EmailTenant       		text,
    EmailOwner        		text,
    EmailId           		text,
    EmailTime 			DATETIME,
    EmailTags         		text,
    EmailSubject      		text,
    EmailFolderName   		text,
    EmailFrom         		text,
    EmailTo           		text,
    EmailCc     		    text,
    EmailInternetMessageId 	text,  -- no index
    EmailBodyPreview  		text,
    EmailImportance   		text,
    EmailConversationId    	text,
    EmailIsRead       		smallint,
    EmailIsDraft      		smallint,
    EmailHasAttachment	 	smallint,
    EmailSize    			integer,
    Plugin      			text,
    FileIndex  				int,
    JobId        			int
);

CREATE INDEX meta_emailowner on MetaEmail (EmailTenant, EmailOwner);
CREATE INDEX meta_emailtime on MetaEmail (EmailTime);
CREATE INDEX meta_emailtags on MetaEmail (EmailTags);
CREATE INDEX meta_emailfoldername on MetaEmail (EmailFolderName);
-- CREATE INDEX meta_emailsender on MetaEmail (EmailSender);
CREATE INDEX meta_emailconversationid on MetaEmail (EmailConversationId);
-- TODO: Implement search op in SQLite
CREATE INDEX meta_emailimportance on MetaEmail (EmailImportance);
CREATE INDEX meta_emailisread on MetaEmail (EmailIsRead);
CREATE INDEX meta_emailhasattachment on MetaEmail (EmailHasAttachment);
CREATE INDEX meta_emailjobid on MetaEmail (Jobid);

CREATE TABLE MetaAttachment
(
    AttachmentTenant            text,
    AttachmentOwner    		text,
    AttachmentName     		text,
    AttachmentEmailId  		text,
    AttachmentContentType 	text,
    AttachmentIsInline 		smallint,
    AttachmentSize    		int,
    Plugin       			text,
    FileIndex    			int,
    JobId        			int
);

CREATE INDEX meta_attachmentowner ON MetaAttachment (AttachmentTenant,AttachmentOwner);
CREATE INDEX meta_attachmentemailid ON MetaAttachment (AttachmentEmailId);
CREATE INDEX meta_attachmentjobid ON MetaAttachment (JobId);

INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
   ('l', 'Doing data despooling',15);
INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
   ('L', 'Committing data (last despool)',15);
INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
   ('u', 'Cloud upload',15);
INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
   ('w', 'Cloud download',15);
INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
   ('q', 'Queued waiting for device',15);
INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
   ('W', 'Terminated normally with warnings',25);
INSERT INTO Events (EventsCode, EventsType, EventsTime, EventsDaemon, EventsSource, EventsRef, EventsText) VALUES
  ('DU0001', 'catalog_update', strftime('%Y-%m-%d %H-%M-%S','now'), '*SHELL*', 'update_bacula_tables', 'pid$$', 'Catalog schema was updated to 1025');
UPDATE Version SET VersionId=1025;
commit;
END-OF-DATA
    then
	echo "Update of Bacula SQLite3 tables 1024 to 1025 succeeded."
	DBVERSION=1025
    else
	echo "Update of Bacula SQLite3 tables 1024 to 1025 failed."
	exit 1
    fi
fi


if [ "$DBVERSION" -eq 1025 ] ; then
    if sqlite3 $* ${db_name}.db <<END-OF-DATA
begin;
CREATE TABLE MalwareMD5
(
    MD5     char(22)    -- Same as File
);
CREATE INDEX malwaremd5_idx on MalwareMD5 (MD5);

CREATE TABLE MalwareSHA256
(
    MD5     char(65)    -- Same as File
);
CREATE INDEX malwaresha256_idx on MalwareSHA256 (MD5);

CREATE TABLE FileEvents 
(
   Id	  INTEGER PRIMARY KEY AUTOINCREMENT,
   Time  DATETIME DEFAULT current_timestamp, -- Time of the detection
   SourceJobId   int,        -- Can be the Verify job id for example, or the jobid during a restore/backup
   JobId         int,        -- JobId where the file was found. Used for pruning
   FileIndex     int,        -- File reference
   Type          char,       -- Event type (antivirus, malware scanning (M), lost file)
   Description   text,       -- Description of the event
   Severity      int,        -- level of severity. (0 OK, 100 Important)
   Source        text        -- Information about the source of the event
);

CREATE INDEX FileEvents_jobid_idx ON FileEvents (JobId, FileIndex);
CREATE INDEX FileEvents_sourcejobid_idx ON FileEvents (SourceJobId);
CREATE INDEX meta_emailid on MetaEmail (EmailId);

ALTER TABLE Object ADD COLUMN FileIndex    integer  not null default 0;

ALTER TABLE Job ADD COLUMN RealStartTime     DATETIME    DEFAULT 0;
ALTER TABLE Job ADD COLUMN isVirtualFull     TINYINT     default 0;
ALTER TABLE Job ADD COLUMN CompressRatio     float       default 0;
ALTER TABLE Job ADD COLUMN Rate              float       default 0;
ALTER TABLE Job ADD COLUMN LastReadStorageId integer     default 0;
ALTER TABLE Job ADD COLUMN LastReadDevice    text        default '';
ALTER TABLE Job ADD COLUMN WriteStorageId    integer     default 0;
ALTER TABLE Job ADD COLUMN WriteDevice       text        default '';
ALTER TABLE Job ADD COLUMN StatusInfo        text        default '';
ALTER TABLE Job ADD COLUMN Encrypted         int         default 0;

ALTER TABLE JobHisto ADD COLUMN RealStartTime     DATETIME    DEFAULT 0;
ALTER TABLE JobHisto ADD COLUMN isVirtualFull     TINYINT     default 0;
ALTER TABLE JobHisto ADD COLUMN CompressRatio     float       default 0;
ALTER TABLE JobHisto ADD COLUMN Rate              float       default 0;
ALTER TABLE JobHisto ADD COLUMN LastReadStorageId integer     default 0;
ALTER TABLE JobHisto ADD COLUMN LastReadDevice    text        default '';
ALTER TABLE JobHisto ADD COLUMN WriteStorageId    integer     default 0;
ALTER TABLE JobHisto ADD COLUMN WriteDevice       text        default '';
ALTER TABLE JobHisto ADD COLUMN StatusInfo        text        default '';
ALTER TABLE JobHisto ADD COLUMN Encrypted         int         default 0;

ALTER TABLE Media ADD COLUMN Protected       TINYINT     DEFAULT 0;
ALTER TABLE Media ADD COLUMN UseProtect      TINYINT     DEFAULT 0;
ALTER TABLE Media ADD COLUMN VolEncrypted    TINYINT     DEFAULT 0;

ALTER TABLE FileSet ADD COLUMN Content    TEXT    DEFAULT '';

INSERT INTO Events (EventsCode, EventsType, EventsTime, EventsDaemon, EventsSource, EventsRef, EventsText) VALUES
  ('DU0001', 'catalog_update', strftime('%Y-%m-%d %H-%M-%S','now'), '*SHELL*', 'update_bacula_tables', 'pid$$', 'Catalog schema was updated to 1026');
UPDATE Version SET VersionId=1026;
commit;
END-OF-DATA
    then
	echo "Update of Bacula SQLite3 tables 1025 to 1026 succeeded."
	DBVERSION=1026
    else
	echo "Update of Bacula SQLite3 tables 1025 to 1026 failed."
	exit 1
    fi
fi
